<?php 
class product_plugin_options extends abstract_product_plugin {

    public function getProductOptions($product_id) {
		$option_data = array();

		$option_query = $this->db->prepare("SELECT * FROM " . $this->db_prefix . "product_option po 
            LEFT JOIN " . $this->db_prefix . "option o 
            ON (po.option_id = o.option_id) 
            LEFT JOIN " . $this->db_prefix . "option_description od 
            ON (o.option_id = od.option_id) 
            WHERE po.product_id = :product_id 
            AND od.language_id = :language_id   
            ORDER BY o.sort_order");

		$option_query->execute(array(
            ':product_id'   => $product_id,
            ':language_id'  => $this->config->get('config_language_id')
        ));
		
		foreach ($option_query->fetchAll(PDO::FETCH_ASSOC) as $option) {
            
            if ($option['type'] == 'select' 
                || $option['type'] == 'radio' 
                || $option['type'] == 'checkbox' 
                || $option['type'] == 'image') {
			
                $option_value_data = array();

				$option_value_query = $this->db->prepare("SELECT * FROM " . $this->db_prefix . "product_option_value pov 
                LEFT JOIN " . $this->db_prefix . "option_value ov 
                ON (pov.option_value_id = ov.option_value_id) 
                LEFT JOIN " . $this->db_prefix . "option_value_description ovd 
                ON (ov.option_value_id = ovd.option_value_id) 
                WHERE pov.product_id        = :product_id  
                AND pov.product_option_id   = :product_option_id 
                AND ovd.language_id         = :language_id  
                ORDER BY ov.sort_order");

				$option_value_query->execute(array(
                    ':product_id'        => $product_id,
                    ':product_option_id' => $option['product_option_id'] ,
                    ':language_id'       => $this->config->get('config_language_id') 
                ));
			
            	$option_value_data = $option_value_query->fetchAll(PDO::FETCH_ASSOC);

				$option_data[] = array(
					'product_option_id' => $option['product_option_id'],
					'option_id'         => $option['option_id'],
					'name'              => $option['name'],
					'type'              => $option['type'],
					'option_value'      => $option_value_data,
					'required'          => $option['required']
				);
			} else {
				$option_data[] = array(
					'product_option_id' => $option['product_option_id'],
					'option_id'         => $option['option_id'],
					'name'              => $option['name'],
					'type'              => $option['type'],
					'option_value'      => $option['option_value'],
					'required'          => $option['required']
				);				
			}
      	}
		
		return $option_data;
	}
}
